<template>
  <div class="tableWithColorTip">
    <div class="thead">
      <div class="tr clearfix">
        <div class="td fl"
          v-for="(item,index) in columns"
          :key="item.feild"
          :style="getStyleByIndex(index)"
        >
          {{ item.title }}
      </div>
      </div>
    </div>
    <div class="tbody" :style="{ maxHeight: getRem(maxHeight) }">
      <div class="tr clearfix" v-for="(row, i) in tableData" :key="row.id">
        <div class="td fl ellipsis" v-for="(item, index) in columns" :key="item.title" :style="getStyleByIndex(index)">
          <i v-if="index === 0" class="colorTip" :style="{ borderTop: '6px solid ' + colors[i] }"></i>
          <span v-if="item.formatter">{{
            item.formatter(row[item.feild])
          }}</span>
          <span v-else :title="row[item.feild]">{{ row[item.feild] }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "TableWithColorTip",
  props: {
    columns: {
      type: Array,
      required: true,
    },
    tableData: {
      type: Array,
      required: true,
      default: [],
    },
    colors: {
      type: Array,
      required: true,
      default: [],
    },
    maxHeight: {
      type: Number | String,
      default: 220
    },
    scale: {
      type: Number,
      default: 1
    }
  },
  data() {
    return {};
  },
  // updated() {
  //   console.log(this.tableData, '???')
  // },
  methods: {
    getRem(val) {
      const fs = parseFloat(window.document.documentElement.style.fontSize)
      return val/fs + 'rem'
    },
    getStyleByIndex(index) {
      let style = {}
      if(index === 0) {
        style = { width: "50%" }
      } else if (index === 1) {
        style = { fontWeight: 700, textAlign: "center", width: "20%", fontFamily: "Arial Rounded MT Bold, Arial Rounded MT Bold-Regular" }
      } else {
        style = { color: "#07C8FF", textAlign: "right", width: "30%" }
      }
      if (this.scale) {
        style.transform = `scale(${this.scale})`
      }
      return style
    }
  }
};
</script>

<style lang="scss" scoped>
.tableWithColorTip {
  width: 100%;
  font-size: 14px;
  line-height: 28px;
  color: #fff;
  border-collapse:collapse;
  border-spacing:0px;
  .thead {
    .tr {
      .td {
        color: #cfeefc !important;
        font-weight: 400 !important;
        margin-left: -10px;
      }
      .td:first-child {
        position: relative;
        padding-left: 18px;
        margin-left: 0;
      }
    }
  }
  .tbody {
    overflow-y: auto;
    .tr {
      background: rgba(0,9,26,0.3);
      margin-bottom: 4px;
      padding-right: 20px;
      .td {
        &:first-child {
          position: relative;
          padding-left: 18px;
        }
      }
    }
  }

  .colorTip {
    position: absolute;
    top: 0;
    left: 0;
    width: 0;
    height: 0;
    border-top: 6px solid red;
    border-bottom: 6px solid transparent;
    border-left: 6px solid transparent;
    border-right: 6px solid transparent;
    transform: rotate(135deg) translate(0px,7px);
  }
}
</style>